package com.mystudy.leetcode.base;

/**
 * @author 周志龙
 * @Email 15216876513@163.com
 * @Date on 2019/7/4 22:10
 * @Description ListNode工具类
 */
public class ListNodeUtils {
    public static ListNode generateLinkList(int[] arrays) {
        ListNode head = null;
        ListNode cur = null;
        for (int i = 0; i < arrays.length; i++) {
            int array = arrays[i];
            if (head == null) {
                head = new ListNode(array);
                cur = head;
            } else {
                cur.next = new ListNode(array);
                cur = cur.next;
            }
        }
        return head;
    }

    /**
     * 生成环形列表
     *
     * @param arrays
     * @param pos
     * @return
     */
    public static ListNode geneCircleLinkList(int[] arrays, int pos) {
        ListNode head = null;
        ListNode cur = null;

        ListNode posNode = null;

        for (int i = 0; i < arrays.length; i++) {

            int array = arrays[i];
            if (head == null) {
                head = new ListNode(array);
                cur = head;
            } else {
                cur.next = new ListNode(array);
                if (i == pos) {
                    posNode = cur.next;
                }
                cur = cur.next;
            }
        }

        cur.next = posNode;

        return head;
    }

    /**
     * 展示循环列表的数据结构
     *
     * @param listNode
     */
    public static void showCircle(ListNode listNode) {
        ListNode cur = listNode;
        StringBuilder sb = new StringBuilder();
        int count = 0;
        while (cur != null) {
            count++;
            sb.append(cur.val);
            sb.append("->");
            cur = cur.next;
            if (count > 6) {
                break;
            }
        }
        sb.append("NULL");
        System.out.println(sb.toString());
    }


    /**
     * 展示链表的数据结构
     *
     * @param listNode
     */
    public static void show(ListNode listNode) {
        ListNode cur = listNode;
        StringBuilder sb = new StringBuilder();
        while (cur != null) {
            sb.append(cur.val);
            sb.append("->");
            cur = cur.next;
        }
        sb.append("NULL");
        System.out.println(sb.toString());
    }

}
